//
//  MBProgressHUD+YYR.m
//  SISSAPP
//
//  Created by 优房网 on 2017/6/29.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "MBProgressHUD+YYR.h"

@implementation MBProgressHUD (YYR)
static int Time = 1; //决定显示的时间

+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
    
    if (view == nil) {
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.labelText  = text ;
    
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:1];
}



+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view afterDelayTime:(NSInteger)delay {
    
    if (view == nil) {
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.labelText  = text ;
    
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    //  随机几秒之后再消失
    [hud hide:YES afterDelay:delay];
}


# pragma mark -成功
+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    [self show:success icon:@"success" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view afterDelay:(NSInteger)delay
{
    [self show:success icon:@"success" view:view afterDelayTime:delay];
}


# pragma mark -错误
+ (void)showError:(NSString *)error toView:(UIView *)view {
    [self show:error icon:@"error" view:view];
}


+ (void)showError:(NSString *)error toView:(UIView *)view afterDelay:(NSInteger)delay {
    [self show:error icon:@"error" view:view afterDelayTime:delay];
}


+ (void)showCustom:(NSString *)text view:(UIView *)view {
    
    if (view == nil) {
        view = [[UIApplication sharedApplication].windows lastObject];
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.dimBackground = NO ; //现实是否遮罩
    
    // 设置图片
    hud.labelText=text;
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading.png"]];
    CABasicAnimation *animation= [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    animation.toValue = [NSNumber numberWithFloat:M_PI*2];
    animation.duration = 1.0;
    animation.repeatCount = 100;
    [img.layer addAnimation:animation forKey:nil];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = img;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
}


#pragma mark 显示一些信息
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = YES;
    return hud;
}

+ (void)showSuccess:(NSString *)success {
    [self showSuccess:success toView:nil];
}

+ (void)showError:(NSString *)error {
    [self showError:error toView:nil];
}

+ (MBProgressHUD *)showMessage:(NSString *)message {
    return [self showMessage:message toView:nil];
}

+ (void)hideHUDForView:(UIView *)view {
    [self hideHUDForView:view animated:YES];
}

+ (void)hideHUD {
    [self hideHUDForView:nil];
}

+ (void)YYR_ShowHUD:(BOOL)animation{
    [self YYR_ShowHUD:nil animation:animation];
}

+ (void)YYR_HideHUD:(BOOL)animation {
    [self YYR_HideHUD:nil animation:animation];
}

+ (void)YYR_ShowHUD:(UIView *)toView animation:(BOOL)animation {
    UIView *view = toView == nil ? [UIApplication sharedApplication].keyWindow : toView;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view ? view : [UIView new] animated:animation];

    hud.color = [UIColor grayColor];
    hud.color = [hud.color colorWithAlphaComponent:0.8];
    
    // 自定义加载动画图片
    hud.mode = MBProgressHUDModeCustomView;
    // 设置图片
    NSMutableArray *imageArray = [NSMutableArray array];
    
    for (int i = 0; i < 40; i ++) {
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"loading%d",i+1]];
        [imageArray addObject:image];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading1"]];
//    imageView.frame = CGRectMake(0, 0, 37, 37);
    imageView.animationImages = imageArray;
    // 图片播放一次所需时长
    imageView.animationDuration = 2.0f;
    // 图片播放次数,0表示无限
    imageView.animationRepeatCount = 0;
    // 执行动画
    [imageView startAnimating];
    hud.customView = imageView;
    
}

+ (void)YYR_HideHUD:(UIView *)toView animation:(BOOL)animation {
    UIView *view = toView == nil ? [UIApplication sharedApplication].keyWindow : toView;
    [MBProgressHUD hideHUDForView:view animated:animation];
}

#pragma mark 显示信息
+ (void)YYR_Show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
    
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.detailsLabelText = text;
    hud.detailsLabelFont = [UIFont systemFontOfSize:15];
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 设置图片
    NSMutableArray *imageArray = [NSMutableArray array];
    NSArray *imageNames = @[@"mainCheckSelected",@"checkSelected"];
    for (int i = 0; i < imageNames.count; i ++) {
        
        UIImage *image = [UIImage imageNamed:imageNames[i]];
        [imageArray addObject:image];
    }
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 37)];
    imageView.animationImages = imageArray;
    //2.2图片播放一次所需时长
    imageView.animationDuration = 5.0f;
    //2.3图片播放次数,0表示无限
    imageView.animationRepeatCount = 1;
    //4.执行动画
    [imageView startAnimating];
    hud.customView = imageView;
    
    
    //    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1秒之后再消失
    [hud hide:YES afterDelay:Time];
}

#pragma mark 显示错误信息
+ (void)YYR_ShowError:(NSString *)error toView:(UIView *)view{
    [self YYR_Show:error icon:@"error.png" view:view];
}

+ (void)YYR_ShowSuccess:(NSString *)success toView:(UIView *)view
{
    [self YYR_Show:success icon:@"success.png" view:view];
}

#pragma mark 显示一些信息
+ (MBProgressHUD *)YYR_ShowMessage:(NSString *)message toView:(UIView *)view {
    if (view == nil) view = [[UIApplication sharedApplication].windows lastObject];
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.labelText = message;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // YES代表需要蒙版效果
    hud.dimBackground = YES;
    return hud;
}

+ (void)YYR_ShowSuccess:(NSString *)success {
    [self YYR_ShowSuccess:success toView:nil];
}

+ (void)YYR_ShowError:(NSString *)error {
    [self YYR_ShowError:error toView:nil];
}
+ (void)YYR_ShowError:(NSString *)error time:(int) time {
    [self YYR_ShowError:error toView:nil];
    Time = time;
}

+ (MBProgressHUD *)YYR_ShowMessage:(NSString *)message {
    return [self YYR_ShowMessage:message toView:nil];
}

+ (void)YYR_HideHUDForView:(UIView *)view {
    [self hideHUDForView:view];
}

+ (void)YYR_HideHUD {
    [self YYR_HideHUDForView:nil];
}


+ (void)YYR_ShowCustomAnimation:(NSString *)msg withImgArry:(NSArray *)imgArry inview:(UIView *)view {
    
    UIImageView *showImageView = [[UIImageView alloc] init];
    showImageView.animationImages = imgArry;
    [showImageView setAnimationRepeatCount:0];
    [showImageView setAnimationDuration:(imgArry.count + 1) * 0.075];
    [showImageView startAnimating];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // 设置图片
    hud.labelText = msg;
    hud.mode = MBProgressHUDModeCustomView;
    hud.customView = showImageView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
}


@end
